package com.fy.controller;


import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fy.aop.LogAnn;
import com.fy.entity.Result;
import com.fy.entity.Search;
import com.fy.entity.Softwarev;
import com.fy.entity.User;
import com.fy.service.ISoftwarevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author 晓岩
 * @since 2022-06-11
 */
@RestController
@RequestMapping("/soft")
public class SoftwarevController {
    @Autowired
    ISoftwarevService softwarevService;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("list")
    @LogAnn
    public Result list(@RequestBody Search search){
        QueryWrapper<Softwarev> wrapper = new QueryWrapper<>();
        if(search.getCname()!=null && !search.getCname().equals("")){
            wrapper.like("cname",search.getCname());
        }
        if(search.getName()!=null && !search.getName().equals("")){
            wrapper.like("name",search.getName());
        }
        if(search.getTime1()!=null){
            wrapper.ge("time",search.getTime1());
        }
        if(search.getTime2()!=null){
            wrapper.le("time",search.getTime2());
        }
        Page page = new Page(search.getPage(), search.getPageSize());
        Page page1 = softwarevService.page(page, wrapper);
        return new Result(200,"",page1);
    }
    @GetMapping("download")
    @LogAnn
    public void download(HttpServletResponse response) throws IOException, UnsupportedEncodingException {
        List<Softwarev> list = softwarevService.list();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Softwarev.class).sheet("模板").doWrite(list);
    }
    @PostMapping("upload")
    @ResponseBody
    @LogAnn
    public String upload(MultipartFile[] file) throws IOException {
        for (MultipartFile multipartFile : file) {
            multipartFile.transferTo(new File("e:/test/"+multipartFile.getOriginalFilename()));
        }
        return "success";
    }
}
